package com.app.business.web.business;

import com.app.business.calculate.CalculateHandInMoneyUtils;
import com.app.business.calculate.CalculateUtils;
import com.app.business.en.Status;
import com.app.business.model.*;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.model.UserInfo;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.AjaxException;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 财务数据管理
 * Created by lsh on 2019/2/14.
 *
 * @author lsh
 */
@Controller
@RequestMapping("/business/finance/*")
@SuppressWarnings("all")
public class FinanceController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private HandInMoneyService handInMoneyService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private ActionHistoryService actionHistoryService;
    @Autowired
    private BuyHousePersonService buyHousePersonService;

    /**
     * 财务复核管理列表
     */
    @RequestMapping("list")
    @RequiresPermissions("finance:list")
    public void list() {
    }

    /**
     * 加载财务复核数据管理列表
     *
     * @return
     */
    @RequestMapping("loadFinanceList")
    @ResponseBody
    @RequiresPermissions("finance:loadFinanceList")
    public Map loadContractList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.contractService.getChoosedHousePageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> checkMap = this.dataDictService.getMapByParentNameForValue("复核状态");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        if (!CollectionUtils.isEmpty(contractList)) {
            Map<String, Object> item = null;
            for (Contract contract : contractList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", contract.getId());
                item.put("town", StringUtils.dataDictShow(townMap.get(contract.getZsProject().getTown())));
                item.put("zsProjectName", contract.getZsProject().getProjectName());
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("limitHouseAreaTotal", BigDecimalUtils.getNotNullStr(contract.getLimitHouseAreaTotal()));
                item.put("financeCheckStatus", IntegerUtils.getNotNull(contract.getFinanceCheckStatus()));
                item.put("financeCheckStatusStr", StringUtils.dataDictShow(checkMap.get(IntegerUtils.getNotNull(contract.getFinanceCheckStatus()).toString())));
                item.put("azSignStatus", IntegerUtils.getNotNull(contract.getAzSignStatus()));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(contract.getAzSignStatus()).toString())));
                listData.add(item);
            }
        }
        return LayUiUtils.page(contractPageBean.getTotalCount().intValue(), listData);
    }


    /**
     * 财务结算表/缴款通知书/缴款清单打印页面
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("financePrint")
    @RequiresPermissions("finance:financePrint")
    public void settlementPrint(HttpServletRequest request, ModelMap modelMap) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        int contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据有误");
        BigDecimal resultArea = new BigDecimal("0");
        if (contract.getBuyStorageRoomType() != null && contract.getBuyStorageRoomType() == 1) {
            resultArea = BigDecimalUtils.getSum(contract.getChooseHouseAreaTotal(), BigDecimalUtils.getBigAndBigDiv(contract.getStorageRoomArea(), contract.getParameter().getCwjBl(), 2));
        } else
            resultArea = contract.getChooseHouseAreaTotal();
        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("resultArea", resultArea);
        modelMap.addAttribute("tkDate", DateUtils.dateToStringFormat(contract.getTkDate(), "yyyy-MM-dd"));
        CalculateHandInMoneyUtils.main(contract.getId());

    }

    /**
     * 设置审核状态（可退回）
     *
     * @param request
     */
    @RequestMapping("checkStatus")
    @ResponseBody
    @RequiresPermissions("finance:checkStatus")
    public synchronized Response checkStatus(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");

        if (contract.getFinanceCheckStatus().equals(Status.NO.getIndex()) || contract.getFinanceCheckStatus().equals(Status.TUI.getIndex())) {
            contract.setFinanceCheckStatus(Status.YES.getIndex());
            contract.setFinanceCheckDate(new Date());
            this.actionHistoryService.saveActionHistoryByContractId(contract.getId(), "设置为财务已审核状态", "财务审核状态");

        } else {
            Precondition.checkAjaxArguement(contract.getFinanceSettleStatus() != null && !contract.getFinanceSettleStatus().equals(Status.YES.getIndex()), "2222", "财务已结算！");
            contract.setFinanceCheckStatus(Status.TUI.getIndex());
            contract.setFinanceCheckDate(new Date());
            this.actionHistoryService.saveActionHistoryByContractId(contract.getId(), "设置为财务审核已退回状态", "财务审核状态");

        }
        this.contractService.save(contract);
        return new Response("0001", "操作成功");
    }

    /**
     * 保存过渡费
     *
     * @param request
     */
    @RequestMapping("setTransitionMoney")
    @ResponseBody
    @RequiresPermissions("finance:setTransitionMoney")
    public synchronized Response setTransitionMoney(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        WebUtils.bind(request, contract);
        this.contractService.save(contract);
        CalculateUtils.main(contract.getId());
        return new Response("0001", "保存成功!");
    }


    /**
     * 财务结算数据管理列表
     */
    @RequestMapping("handInMoneyList")
    @RequiresPermissions("finance:handInMoneyList")
    public void handInMoneyList() {
    }

    /**
     * 加载结算财务数据管理列表
     *
     * @return
     */
    @RequestMapping("loadHandInMoneyList")
    @ResponseBody
    @RequiresPermissions("finance:loadHandInMoneyList")
    public Map loadHandInMoneyList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<Contract> contractPageBean = this.contractService.getAzSignAndFhPageBean();
        List<Contract> contractList = contractPageBean.getDatas();
        Map<String, DataDict> signMap = this.dataDictService.getMapByParentNameForValue("签约状态");
        Map<String, DataDict> settleMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");
        Map<String, DataDict> daffMap = this.dataDictService.getMapByParentNameForValue("档案发放状态");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        if (!CollectionUtils.isEmpty(contractList)) {
            Map<String, Object> item = null;
            for (Contract contract : contractList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", contract.getId());
                item.put("town", StringUtils.dataDictShow(townMap.get(contract.getZsProject().getTown())));
                item.put("zsProjectName", contract.getZsProject().getProjectName());
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                item.put("name", StringUtils.getNotNull(contract.getName()));
                item.put("chooseHouseNum", StringUtils.getNotNull(contract.getChooseHouseNum()));
                item.put("limitHouseAreaTotal", BigDecimalUtils.getNotNullStr(contract.getLimitHouseAreaTotal()));
                item.put("azSignStatus", IntegerUtils.getNotNull(contract.getAzSignStatus()));
                item.put("azSignStatusStr", StringUtils.dataDictShow(signMap.get(IntegerUtils.getNotNull(contract.getAzSignStatus()).toString())));
                item.put("financeSettleStatus", IntegerUtils.getNotNull(contract.getFinanceSettleStatus()));
                item.put("financeSettleStatusStr", StringUtils.dataDictShow(settleMap.get(IntegerUtils.getNotNull(contract.getFinanceSettleStatus()).toString())));
                item.put("daffStatus", IntegerUtils.getNotNull(contract.getDaffStatus()));
                item.put("daffStatusStr", StringUtils.dataDictShow(daffMap.get(IntegerUtils.getNotNull(contract.getDaffStatus()).toString())));
                listData.add(item);
            }
        }
        return LayUiUtils.page(contractPageBean.getTotalCount().intValue(), listData);
    }


    /**
     * 结清凭证/户型清单/交房通知书打印页面
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("handInMoneyPrint")
    @RequiresPermissions("finance:handInMoneyPrint")
    public void handInMoneyPrint(HttpServletRequest request, ModelMap modelMap) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        int contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据有误");
        List<House> houseList = new ArrayList<>();
        List<CarLot> carLotList = new ArrayList<>();
        List<StorageRoom> storageRoomList = new ArrayList<>();
        if (contract.getFinalMoney().compareTo(new BigDecimal("0")) < 0) {
            houseList = this.houseService.getHandInMoneyHouseListByContractId(contract.getId());
            carLotList = this.carLotService.getHandInMoneyCarLotListByContractId(contract.getId());
            storageRoomList = this.storageRoomService.getHandInMoneyStorageRoomListByContractId(contract.getId());
        } else {
            houseList = this.houseService.getHouseListByContractId(contract.getId());
            carLotList = this.carLotService.getCarLotListByContractId(contract.getId());
            storageRoomList = this.storageRoomService.getStorageRoomListByContractId(contract.getId());
        }
        Map<String, List<Map>> buyHousePersonHouseMap = this.buyHousePersonService.getBuyHousePersonByHouse();
        Map<String, List<Map>> buyHousePersonCarLotMap = this.buyHousePersonService.getBuyHousePersonByCarLot();
        Map<String, List<Map>> buyHousePersonStorageRoomMap = this.buyHousePersonService.getBuyHousePersonByStorageRoom();

        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("carLotList", carLotList);
        modelMap.addAttribute("storageRoomList", storageRoomList);
        modelMap.addAttribute("buyHousePersonHouseMap", buyHousePersonHouseMap);
        modelMap.addAttribute("buyHousePersonCarLotMap", buyHousePersonCarLotMap);
        modelMap.addAttribute("buyHousePersonStorageRoomMap", buyHousePersonStorageRoomMap);
        CalculateHandInMoneyUtils.main(contract.getId());


    }

    /**
     * 加载缴款信息
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("handInMoneyRecordList")
    @RequiresPermissions("finance:handInMoneyRecordList")
    public void handInMoneyRecordList(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Map cwMemoMap= CalculateHandInMoneyUtils.calQiHouseMoney(contract);
        BigDecimal houseYjMoney = BigDecimal.ZERO;
        if (contract.getHouseFinalNoWxjMoney().compareTo(BigDecimal.ZERO) < 0) {
            houseYjMoney = contract.getHouseFinalMoney();
        }
        modelMap.put("bigUtils", new BigDecimalUtils());
        modelMap.put("houseYjMoney",houseYjMoney);
        modelMap.addAttribute("cwMemoMap", cwMemoMap);
        modelMap.addAttribute("contract", contract);
    }

    /**
     * 加载缴款信息
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("handInMoney")
    @RequiresPermissions("finance:handInMoney")
    public void handInMoney(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Integer handInMoneyId = ParamUtils.getInt(request, "handInMoneyId", 0);
        Contract contract = this.contractService.findOne(contractId);
        HandInMoney handInMoney = this.handInMoneyService.findOne(handInMoneyId);

        //缴款合计  公维金先缴
        BigDecimal yjMoney = BigDecimal.ZERO;

        if (contract.getHouseFinalNoWxjMoney().compareTo(BigDecimal.ZERO) < 0) {
            yjMoney = BigDecimalUtils.getSum(yjMoney,contract.getHouseFinalNoWxjMoney());
        }

        //公维金肯定为负数
        yjMoney = BigDecimalUtils.getSum(yjMoney,contract.getZzwxMoney());
        modelMap.put("yjMoney",BigDecimalUtils.getAbsolute(yjMoney));
        BigDecimal syMoney = BigDecimalUtils.getBigAndBigSub(BigDecimalUtils.getAbsolute(yjMoney), contract.getHandInMoney());

        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("handInMoney", handInMoney);
        modelMap.addAttribute("syMoney", syMoney);
    }


    /**
     * 登记缴款数据保存
     *
     * @param request
     * @return
     * @throws AjaxException
     */
    @RequestMapping("handInMoneySave")
    @ResponseBody
    @RequiresPermissions("finance:handInMoneySave")
    public synchronized Response handInMoneySave(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Response response = this.handInMoneyService.handInMoneySave(request);
        return response;
    }

    /**
     * 删除缴款数据
     *
     * @param request
     * @return
     */
    @RequestMapping("delHandInMoneyById")
    @ResponseBody
    @RequiresPermissions("finance:delHandInMoneyById")
    public synchronized Response delHandInMoneyById(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer handInMoneyId = ParamUtils.getInt(request, "handInMoneyId", 0);
        HandInMoney handInMoney = this.handInMoneyService.findOne(handInMoneyId);
        Precondition.checkAjaxArguement(handInMoney != null, "1111", "数据错误！");
        Contract contract = handInMoney.getContract();
        Precondition.checkAjaxArguement(contract != null, "2222", "数据错误！");
        this.handInMoneyService.delete(handInMoney.getId());
        CalculateHandInMoneyUtils.main(contract.getId());
        return new Response("0001", "删除成功!");
    }

    /**
     * 加载缴款记录
     *
     * @return
     */
    @RequestMapping("loadHandInMoneyRecord")
    @ResponseBody
    @RequiresPermissions("finance:loadHandInMoneyRecord")
    public Map loadHandInMoneyRecord(HttpServletRequest request, ModelMap modelMap) {
        List<Map> listData = Lists.newArrayList();
        int contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        List<HandInMoney> handInMoneyList = this.handInMoneyService.getHandInMoneyListByContractId(contractId);//缴款记录
        Map<String, DataDict> handInMoneyMap = this.dataDictService.getMapByParentNameForValue("缴款类别");
        if (CollectionUtils.isNotEmpty(handInMoneyList)) {
            Map<String, Object> item = null;
            for (HandInMoney handInMoney : handInMoneyList) {
                item = null;
                item = Maps.newHashMap();
                item.put("id", handInMoney.getId());
                item.put("handInMoneyDate", DateUtils.dateToStringDefault(handInMoney.getHandInMoneyDate()));
                item.put("handInMoneyType", IntegerUtils.getNotNull(handInMoney.getHandInMoneyType()));
                item.put("handInMoneyTypeStr", StringUtils.dataDictShow(handInMoneyMap.get(IntegerUtils.getNotNull(handInMoney.getHandInMoneyType()).toString())));
                item.put("handInMoney", BigDecimalUtils.getNotNullStr(handInMoney.getHandInMoney()));
                item.put("handInMoneyMemo", StringUtils.getNotNull(handInMoney.getHandInMoneyMemo()));
                item.put("contractCode", StringUtils.getNotNull(contract.getContractCode()));
                listData.add(item);
            }
        }
        return LayUiUtils.list(listData);
    }


    /**
     * 设置协议结算状态（可撤销）
     *
     * @param request
     */
    @RequestMapping("setFinanceSettleStatus")
    @ResponseBody
    @RequiresPermissions("finance:setFinanceSettleStatus")
    public synchronized Response setFinanceSettleStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        String str = null;
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        Precondition.checkAjaxArguement(contract.getLimitStatus() != 3, "1111", "该户不可结算交房，请确认无误后联系技术支持修改！");
        BigDecimal syMoney = BigDecimalUtils.getSum(contract.getHandInMoney(), contract.getHouseFinalMoney());
        if (contract.getFinanceSettleStatus().equals(Status.NO.getIndex())) {
            if (contract.getHouseFinalMoney().compareTo(new BigDecimal("0")) < 0) {
/*
                Precondition.checkAjaxArguement(syMoney.compareTo(new BigDecimal("0")) <= 0, "2222", "该户尚未缴清款项,请先登记缴款！");
*/
            }
            contract.setFinanceSettleStatus(Status.YES.getIndex());
            contract.setFinanceSettleDate(new Date());
            contract.setDaffStatus(Status.YES.getIndex());
            contract.setDaffDate(new Date());
            str = "设置为财务已结算、档案已发放";
        } else if (contract.getFinanceSettleStatus().equals(Status.YES.getIndex())) {
            Precondition.checkAjaxArguement(contract.getDaffStatus().equals(Status.NO.getIndex()), "2222", "该家庭已签约并发放协议档案，不允许撤销！");
            contract.setFinanceSettleStatus(Status.NO.getIndex());
            contract.setFinanceSettleDate(null);
            str = "设置为财务未结算";

        } else if (contract.getFinanceSettleStatus().equals(Status.TUI.getIndex())) {
/*
            Precondition.checkAjaxArguement(syMoney.compareTo(new BigDecimal("0")) <= 0, "2222", "该户尚未缴清款项,请先登记缴款！");
*/
            contract.setFinanceSettleStatus(Status.YES.getIndex());
            contract.setFinanceSettleDate(new Date());
            contract.setDaffStatus(Status.YES.getIndex());
            contract.setDaffDate(new Date());
            str = "设置为财务已结算、档案已发放";
        }
        this.contractService.save(contract);
        this.actionHistoryService.saveActionHistoryByContractId(contract.getId(), str, "财务结算状态");
        this.contractService.setHouseFinanceSettleStatus(contract);
        return new Response("0001", "操作成功");
    }

    /**
     * 财务缴款登记房源、车位管理列表
     */
    @RequestMapping("setHouseSettleStatusList")
    @RequiresPermissions("finance:setHouseSettleStatusList")
    public void setHouseSettleStatusList(HttpServletRequest request, ModelMap modelMap) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkSqdsArguement(contract != null, "数据有误,请联系技术支持");
        List<House> houseList = this.houseService.getHouseListByContractId(contract.getId());
        List<CarLot> carLotList = this.carLotService.getCarLotListByContractId(contract.getId());
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        Map<String, DataDict> financeMap = this.dataDictService.getMapByParentNameForValue("财务结清状态");


        modelMap.addAttribute("houseList", houseList);
        modelMap.addAttribute("carLotList", carLotList);
        modelMap.addAttribute("contract", contract);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        modelMap.addAttribute("carLotTypeMap", carLotTypeMap);
        modelMap.addAttribute("financeMap", financeMap);
    }


    /**
     * 设置房源结算状态（可撤销）
     *
     * @param request
     */
    @RequestMapping("setHouseFinanceSettleStatus")
    @ResponseBody
    @RequiresPermissions("finance:setHouseFinanceSettleStatus")
    public synchronized Response setHouseFinanceSettleStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
        House house = this.houseService.findOne(houseId);
        Integer contractId = house.getContract().getId();
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        Precondition.checkAjaxArguement(contract.getLimitStatus() != 3, "1111", "该户不可结算交房，请确认无误后联系技术支持修改！");
        Precondition.checkAjaxArguement(house != null, "1111", "数据错误！");
        this.houseService.setHouseFinanceSettleStatus(house);
        this.contractService.setFinanceSettleStatus(contract);
        return new Response("0001", "操作成功");
    }

    /**
     * 设置车位结算状态（可撤销）
     *
     * @param request
     */
    @RequestMapping("setCarLotFinanceSettleStatus")
    @ResponseBody
    @RequiresPermissions("finance:setCarLotFinanceSettleStatus")
    public synchronized Response setCarLotFinanceSettleStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer carLotId = ParamUtils.getInt(request, "carLotId", 0);
        CarLot carLot = this.carLotService.findOne(carLotId);
        Integer contractId = carLot.getContract().getId();
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        Precondition.checkAjaxArguement(contract.getLimitStatus() != 3, "1111", "该户不可结算交房，请确认无误后联系技术支持修改！");
        Precondition.checkAjaxArguement(carLot != null, "1111", "数据错误！");
        this.carLotService.setCarLotFinanceSettleStatus(carLot);
        this.contractService.setFinanceSettleStatus(contract);
        return new Response("0001", "操作成功");
    }


    /**
     * 设置档案状态（不可撤销）
     *
     * @param request
     */
    @RequestMapping("setDaffStatus")
    @ResponseBody
    @RequiresPermissions("finance:setDaffStatus")
    public synchronized Response setDaffStatus(HttpServletRequest request) {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "1111", "数据错误！");
        Precondition.checkAjaxArguement(!contract.getDaffStatus().equals(Status.YES.getIndex()), "2222", "该协议档案已发放，不允许撤销！");

        contract.setDaffStatus(Status.YES.getIndex());
        contract.setDaffDate(new Date());

        this.contractService.save(contract);
        this.actionHistoryService.saveActionHistoryByContractId(contract.getId(), "财务结算页面设置为档案已发放", "档案发放状态");

        return new Response("0001", "操作成功");
    }

}